package com.cs252.flashcardz;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.app.ExpandableListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.ExpandableListAdapter;
import android.widget.TextView;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.Toast;

public class SubjectList extends Activity implements OnClickListener{
	public Button create, back;
	public String titleText, frontText, backText;
	ExpandableListAdapter listAdapter;
	ExpandableListView listView;
	List<String> listDataHeader;
	HashMap<String, List<String>> listDataChild;
		
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list_view);
		
		create = (Button) findViewById(R.id.create);
		back = (Button) findViewById(R.id.back);
		create.setOnClickListener(this);
		back.setOnClickListener(this);
		
		listView = (ExpandableListView) findViewById(R.id.newExList);
		prepareListData();
		listAdapter = new com.cs252.flashcardz.ExpandableListAdapter(this, listDataHeader, listDataChild);
		listView.setAdapter(listAdapter);
		
		listView.setOnChildClickListener(new OnChildClickListener() {
			 
            @Override
            public boolean onChildClick(ExpandableListView parent, View v,
                    int groupPosition, int childPosition, long id) {
            	String delims = "[%@]";
            	String[] tokens = listDataChild.get(listDataHeader.get(groupPosition)).get(childPosition).split(delims);
                titleText = tokens[0];
                frontText = tokens[1];
                backText = tokens[2];
            	// TODO Auto-generated method stub
            	Thread changeAct = new Thread(){
    				public void run(){
    					Intent card = new Intent(getBaseContext(), ReadCard.class);
    					card.putExtra("TITLE", titleText);
    					card.putExtra("FRONT", frontText);
    					card.putExtra("BACK", backText);
    					startActivity(card);    					
    				}
    			};	
    			changeAct.start();
                return false;
            }
        });
	}

	private void prepareListData(){
		CallDatabase retrieve = new CallDatabase();
		List<String> temp;
		int count = 0;
		listDataHeader = retrieve.getSubjects();
		listDataChild = new HashMap<String, List<String>>();
		while(count < listDataHeader.size()){
			temp = retrieve.getFlashCards(listDataHeader.get(count));
			listDataChild.put(listDataHeader.get(count), temp);
			count++;
		}		
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if(v.getId() == R.id.create){
			Thread newCard = new Thread(){
				public void run(){
					Intent createCard = new Intent("com.cs252.flashcardz.MAKECARD");
					startActivity(createCard);
				}
			};
			newCard.start();
			
		}else if(v.getId() == R.id.back){			
			Thread login = new Thread(){
				public void run(){
					Intent openLogin = new Intent("com.cs252.flashcardz.OPENING");
					startActivity(openLogin);
					
				}
			};	
			login.start();

		}		
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		finish();
	}

	
	
}
